import * as shelfService from '../../services/shelfUp';
import {getJoberNum} from '../../services/shelfDown';
import {queryAssetType} from '../../services/assetType';
import {qryDepts,qryAllUser} from '../../services/user';
import {queryAssetBrand,queryAssetModelAll} from '../../services/assetModel';
import {queryRoomList} from '../../services/tRoom';
import {qryRackList,querAssetListByParam} from '../../services/equipment';

export default {
  namespace: 'shelfUp',
  state: {
    list: [],
    assetTypeTree:[],
    roomList:[],
    assetBrandList:[],
    assetModelList:[],
    userList:[],
    assetList:[],
    rackList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    filePath:"",
    newKey : '',
    detailList:[],
    currentItem: {},
    viewModalVisible:false,
    viewItem:{},
    viewDetailList:[],
    detailModalVisible:false,
    pullOffId:"",
    instockModalVisible:false,
    instockItem:{},
    instockItemEdit:{},
    instockModalType:'createInstock',
    secondModalVisible: false,
    editModalVisible: false,
    instockEditModalVisible: false,
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 
    assetItem:{},
  },
  reducers: {
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    queryListByParamSuc(state, { payload }) {
      return { ...state, ...payload };
    },    
    changeDetailList(state, { payload }) {
      return { ...state,  ...payload, secondModalVisible: false };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {      
      return { ...state, assetBrandList };
    },
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    updateDetailListSuc(state,{payload}) {
      return { ...state, ...payload }
    },
    queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    getJoberNumSuc(state, { payload}) {
      return { ...state, pullOffId:payload };
    },
    qryRackListSuc(state, { payload: rackList }) {
      return { ...state, rackList };
    },
    showModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, detailList:[], newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
      return { ...state, ...payload, viewModalVisible: true }
    },   
    hideViewModal (state) {
      return { ...state, viewModalVisible: false, }
    },
    showCreateDetailModal (state, { payload }) {
      return { ...state, ...payload, detailModalVisible: true }
    },
    hideCreateDetailModal (state, { payload }) {
      return { ...state, ...payload,detailModalVisible: false }
    },
    showInstockModal (state, { payload }) {
      return { ...state, ...payload,instockModalVisible: true }
    },
    hideInstockModal (state) {
      return { ...state,instockModalVisible: false }
    },
    updateState (state, { payload }) {
      return { ...state, ...payload}
    },
    //弹出上架页面
    showInstockEditModal (state, { payload }) {
       return { ...state, ...payload,newKey : new Date().getTime() + '' ,instockEditModalVisible: true }
    },
    //隐藏上架页面
    hideInstockEditModal (state) {
      return { ...state,newKey : new Date().getTime() + '' , instockEditModalVisible: false }
    },
    showSecondModal (state, { payload }) {
      return { ...state, ...payload, secondModalVisible: true }
    },
    hideSecondModal (state) {
      return { ...state,secondModalVisible: false }
    },
    showEditModal (state, { payload }) {
      return { ...state, ...payload,  editModalVisible: true }
    },
    hideEditModal (state) {
      return { ...state, editModalVisible: false}
    },  
    
  },
  effects: {  
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryRoom({ payload },{ call, put }) {
      const { body } = yield call(queryRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    }, 

    *queryList({ payload },{ call, put }) {
      const { body } = yield call(shelfService.queryList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(getJoberNum,payload);
      yield put({ type: 'getJoberNumSuc', payload: body.data, });
      yield put({type: 'showModal'});
      yield put({type: 'queryAssetType'});
      yield put({ type: 'queryAssetBrand', payload: {  },});     
    },
    *queryAssetList({ payload }, { call, put }) {     
      yield put({ type: 'querAssetListByParam', payload: { roomId: payload.roomId,searchStatusType:2} });
      yield put({ type: 'showSecondModal', payload: { assetItem: payload} });
    },
    *onViewItem({ payload },{ call, put }){
      const { body } = yield call(shelfService.qryById,payload);
      yield put({type: 'showViewModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},
      });
    }, 
    *onEditPutOnDetail({ payload },{ call, put }){
      const { body } = yield call(shelfService.qryById,payload);
      yield put({type: 'showEditModal', payload: {viewItem: body.data, viewDetailList:body.dataVo},});     
      yield put({ type: 'qryRackList',payload:body.data.roomId_});
    }, 

    *querAssetListByParam({ payload },{ call, put }){
      const { body } = yield call(shelfService.querAssetListByParam,payload);
      yield put({ type: 'queryListByParamSuc', payload: { assetList:body.data,assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});      
    },
    *addShelfUp({ payload },{ call, put }){
      const { body } = yield call(shelfService.create,payload);
      yield put({ type: 'hideModal' });
      yield put({ type: 'reload' });
    },
    *editShelfUp({ payload },{ call, put }){
      const { body } = yield call(shelfService.editShelfUp,payload);
      yield put({ type: 'hideEditModal' });
      yield put({ type: 'reload' });
    },
    *qryRackList({ payload },{ call, put }) {     
      const { body }  = yield call(qryRackList,payload);
      yield put({ type: 'qryRackListSuc', payload:body.data});
    },
    *updateDetailList({ payload },{ call, put }) { 
      yield put({ type: 'updateDetailListSuc', payload});
      yield put({ type: 'hideInstockModal' });
    },
    //修改完机柜和U位信息更新列表里的信息
    *updateViewDetailList({ payload },{ call, put }) { 
      yield put({ type: 'updateDetailListSuc', payload});
      yield put({ type: 'hideInstockEditModal' });
    },
    *qryAssetById({ payload },{ call, put }){     
      const { body } = yield call(shelfService.qryAssetById,payload);
      yield put({
           type: 'showInstockModal',
           payload: {
              instockItem: body.data,
              instockModalType: 'editInstock',
            },});
    },
    *getInstockData({ payload },{ call, put }){ 
      yield put({
           type: 'showInstockEditModal',
           payload: {
              instockItemEdit: payload,
              instockModalType: 'editInstock',
            },
      });    
    },       
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(queryAssetModelAll,payload);
      yield put({ type: 'updateState', payload: {assetModelList:body.data} });
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(shelfService.edit,payload);
      yield put({ type: 'reload' });
    },
    *remove({ payload },{ call, put }){
      const { body } = yield call(shelfService.remove,payload.id);
      yield put({ type: 'queryList', payload:payload.search });
    },
    *reload(action, { put }) {
      yield put({ type: 'queryList', payload:{ 'name':'' }});
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/shelf/up') {         
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryRoom' });          
          dispatch({ type: 'queryList', payload:{ 'status':'' }});
        }
      });
    },
  },
};
